package com.qifenqian.turbot.module.adapter.order

import android.text.SpannableString
import android.text.Spanned
import android.text.method.LinkMovementMethod
import android.widget.TextView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.qifenqian.turbot.R
import com.qifenqian.turbot.module.entity.response.order.OrderOnlineEntity
import com.qifenqian.turbot.module.entity.response.order.OrderQueueEntity
import kotlinx.android.synthetic.main.item_order_reception_orders.view.*
import kotlinx.android.synthetic.main.item_order_reception_queue.view.*
import kotlinx.android.synthetic.main.view_textview.view.*

/**
 * @author H
 * @createTime 2019/1/10 10:41
 * @description 接待界面的3个adapter
 * @see
 * @since
 */

class OrderReceptionAdapter(var tag: Int, layoutResId: Int, data: List<Any>) : BaseQuickAdapter<Any, BaseViewHolder>(layoutResId, data) {

    companion object {
        //区分3个RecyclerView的Adapter
        const val ADAPTER_1 = 0x534
        const val ADAPTER_2 = 0x742
        const val ADAPTER_3 = 0x417
    }

    override fun convert(helper: BaseViewHolder, item: Any) {
        val view = helper.itemView
        when (tag) {
            ADAPTER_1 -> {
                item as String
                view.tv.text = item
            }
            ADAPTER_2 -> {
                item as OrderQueueEntity
                view.tv_item_order_reception_queue_number.text = item.number
                view.tv_item_order_reception_queue_target.setSpannableText(item.target, "${item.tableNumber}号")
                view.btn_item_order_reception_queue_pass.setOnClickListener {
                    if (mOnChildClickListener != null) {
                        mOnChildClickListener!!.onClick(helper.layoutPosition)
                    }
                }
            }
            ADAPTER_3 -> {
                item as OrderOnlineEntity
                view.tv_item_order_reception_orders_phone.text = item.phone
                view.tv_item_order_reception_orders_people_count.text = "${item.minPeopleCount}-${item.maxPeopleCount}人"
                view.tv_item_order_reception_orders_time.text = item.time
            }
        }
    }

    private fun TextView.setSpannableText(s: String, lightText: String) {
        val spannableString = SpannableString(s)
        spannableString.setSpan(null, s.indexOf(lightText), s.indexOf(lightText) + lightText.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
        movementMethod = LinkMovementMethod.getInstance()
        highlightColor = resources.getColor(R.color.transparent)
        text = spannableString
    }

    private var mOnChildClickListener: OnChildClickListener? = null

    fun setOnChildClickListener(listener: OnChildClickListener) {
        mOnChildClickListener = listener
    }

    interface OnChildClickListener {
        fun onClick(position: Int)
    }
}
